#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Agresso.Interface.Layer.Domain;
using System.Web;
using Agresso.Interface.Layer.Service;
using System.Collections;

#endregion

namespace Agresso.Interface.Layer.Web
{
    public partial class SoftmarVesselMappingForm : Form, IProfitCentreParentForm, IVesselMappingForm
    {
        Vessel m_vessel;
        UserControlSoftmarVesselMapping m_parent;

        public SoftmarVesselMappingForm(UserControlSoftmarVesselMapping parent)
        {
            InitializeComponent();
            Vessel vessel = new Vessel();
            vessel.ProfitCentre = new ProfitCentre();
            this.Text = "Vessel Agresso - Softmar Mapping";
            m_vessel = vessel;
            m_parent = parent;
            textBoxVesselName.ReadOnly = false;
            textBoxVesselCode.ReadOnly = false;
            textBoxYearOfBuild.ReadOnly = false;
            textBoxFlag.ReadOnly = false;
            textBoxDWT.ReadOnly = false;
            textBoxSizeClass.ReadOnly = false;
            textBoxCharterType.ReadOnly = false;
            textBoxVesselType.ReadOnly = false;
        }
        public SoftmarVesselMappingForm(Vessel vessel, UserControlSoftmarVesselMapping parent)
        {
            InitializeComponent();
            this.Text = "Vessel Agresso - Softmar Mapping";
            m_vessel = vessel;
            m_parent = parent;
            textBoxVesselName.Text = vessel.vessel_name;
            textBoxVesselCode.Text = vessel.vessel_code;
            textBoxYearOfBuild.Text = vessel.year_of_build;
            textBoxFlag.Text = vessel.flag;
            textBoxDWT.Text = vessel.dwt;
            textBoxSizeClass.Text = vessel.size_class;
            textBoxCharterType.Text = vessel.charter_type;
            textBoxAgrDescription.Text = vessel.agr_description;
            textBoxAgrDimValue.Text = vessel.agr_dim_value;
            textBoxAgrRelValue.Text = vessel.agr_rel_value;
            textBoxVesselType.Text = vessel.vessel_type;
            textBoxCLient.Text = vessel.client;
            if (vessel.ProfitCentre.profit_centre_id > 0)
            {
                ProfitCentreService service = new ProfitCentreService();
                vessel.ProfitCentre = service.GetProfitCentreByID(vessel.ProfitCentre.profit_centre_id);
                textBoxProfitCentre.Text = vessel.ProfitCentre.agr_description;
            }
        }
        public SoftmarVesselMappingForm(string vesselName)
        {
            InitializeComponent();
            this.Text = "Vessel Agresso - Softmar Mapping";
            VesselService vsvc = new VesselService();
            m_vessel = vsvc.GetVesselMappingByVesselName(vesselName);
            textBoxVesselName.Text = m_vessel.vessel_name;
            textBoxVesselCode.Text = m_vessel.vessel_code;
            textBoxYearOfBuild.Text = m_vessel.year_of_build;
            textBoxFlag.Text = m_vessel.flag;
            textBoxDWT.Text = m_vessel.dwt;
            textBoxSizeClass.Text = m_vessel.size_class;
            textBoxCharterType.Text = m_vessel.charter_type;
            textBoxAgrDescription.Text = m_vessel.agr_description;
            textBoxAgrDimValue.Text = m_vessel.agr_dim_value;
            textBoxAgrRelValue.Text = m_vessel.agr_rel_value;
            textBoxVesselType.Text = m_vessel.vessel_type;
            textBoxCLient.Text = m_vessel.client;
            if (m_vessel.ProfitCentre.profit_centre_id > 0)
            {
                ProfitCentreService service = new ProfitCentreService();
                m_vessel.ProfitCentre = service.GetProfitCentreByID(m_vessel.ProfitCentre.profit_centre_id);
                textBoxProfitCentre.Text = m_vessel.ProfitCentre.agr_description;
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_vessel.vessel_id > 0)
                    UpdateSubject();
                else
                    InsertSubject();
                this.Close();
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void InsertSubject()
        {
            m_vessel.vessel_name = textBoxVesselName.Text;
            m_vessel.vessel_code = textBoxVesselCode.Text;
            m_vessel.year_of_build = textBoxYearOfBuild.Text;
            m_vessel.flag = textBoxFlag.Text;
            m_vessel.dwt = textBoxDWT.Text;
            m_vessel.size_class = textBoxSizeClass.Text;
            m_vessel.charter_type = textBoxCharterType.Text;
            m_vessel.vessel_type = textBoxVesselType.Text;
            m_vessel.agr_description = textBoxAgrDescription.Text;
            m_vessel.agr_dim_value = textBoxAgrDimValue.Text;
            m_vessel.agr_rel_value = textBoxAgrRelValue.Text;
            m_vessel.client = textBoxCLient.Text;
            VesselService service = new VesselService();
            service.InsertVesselMapping(m_vessel);
            if (m_vessel.vessel_id == 0)
                throw new Exception("Vessel Code already exist!");
            if (m_parent != null)
                m_parent.InsertNewItem(m_vessel);
        }

        private void UpdateSubject()
        {
            m_vessel.agr_description = textBoxAgrDescription.Text;
            m_vessel.agr_dim_value = textBoxAgrDimValue.Text;
            m_vessel.agr_rel_value = textBoxAgrRelValue.Text;
            m_vessel.client = textBoxCLient.Text;
            VesselService service = new VesselService();
            service.UpdateVesselMapping(m_vessel);
            if (m_parent != null)
                m_parent.UpdateSelectedItem(m_vessel);
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void textBoxAgrDescription_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            buttonApply.Enabled = true;
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_vessel.vessel_id > 0)
                    UpdateSubject();
                else
                    InsertSubject();
                buttonApply.Enabled = false;
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void SoftmarVesselMappingForm_Load(object sender, EventArgs e)
        {
            if (m_vessel.vessel_id > 0)
            {
                textBoxAgrDescription.Focus();
                textBoxAgrDescription.SelectAll();
            }
            else
            {
                textBoxVesselName.Focus();
                textBoxVesselName.SelectAll();
            }
        }

        private void buttonFindProfitCentre_Click(object sender, EventArgs e)
        {
            ProfitCentreListForm form = new ProfitCentreListForm(m_vessel.ProfitCentre, this);
            form.ShowDialog();
        }

        void IProfitCentreParentForm.UpdateProfitCentre(ProfitCentre a)
        {
            m_vessel.ProfitCentre = a;
            textBoxProfitCentre.Text = a.agr_description;
            buttonApply.Enabled = true;
        }

        private void buttonDeleteProfitCentre_Click(object sender, EventArgs e)
        {
            m_vessel.ProfitCentre = new ProfitCentre(0);
            textBoxProfitCentre.Text = "";
            buttonApply.Enabled = true;
        }

        private void buttonFindVesselAgresso_Click(object sender, EventArgs e)
        {
            FindVesselForm form = new FindVesselForm(this);
            form.ShowDialog();
        }

        public string GetVesselName()
        {
            return textBoxVesselName.Text;
        }

        public void UpdateAgressoVessel(agldimvalue acu)
        {
            textBoxAgrDescription.Text = acu.description;
            textBoxAgrDimValue.Text = acu.dim_value;
            textBoxAgrRelValue.Text = acu.rel_value;
            textBoxCLient.Text = acu.client;
        }
    }
}